package gui.client;

import java.awt.Color;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Time;
import java.text.ParseException;
import java.util.Date;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.text.MaskFormatter;

import system.FieldValidator;
import system.SystemMode;
import system.SystemTypes;
import system.VCP;
import vcpClasses.Order;

import javax.swing.JEditorPane;
import javax.swing.JTextField;

import control.LocalOpertaionController;
import control.LoginController;
import control.OrderController;
/**
 * this class use to Track Order Screen 
 * @author G8
 *
 */
@SuppressWarnings("serial")
public class TrackOrdeScreen extends JPanel implements ActionListener
{
	public JButton btnLogOut;
	public JButton btnBack;
	public JButton btnSave;
	public JButton btnCancelOrder;
	private JTextField VehicleNumber;
	private JTextField idNumber;
	private JLabel lblVehicleNumber;
	private OrderController orderControl;
	private OrderController trackControl;
	private JButton btnEnter;
	private LocalOpertaionController localOpControl;
	/**
	 *Track Orde Screen Constructor 
	 */
	public TrackOrdeScreen()
	{
		super();
		initialize();
	}
	/**
	 * This method contains the view of the Panel
	 */
	private void initialize()
	{
		localOpControl = new LocalOpertaionController(null);
		orderControl=new OrderController(null);
		
		setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Existing Order Screen", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		setBackground(SystemColor.menu);
		setBounds(VCP.guiX,VCP.guiY,500,400);
		setLayout(null);
		VCP.frame.setBounds(this.getBounds());
		
		btnBack = new JButton("Back");
		btnBack.setIcon(new ImageIcon(AdvanceOrderScreen.class.getResource("/img/back.png")));
		btnBack.setSize(150, 30);
		btnBack.setBounds(281, 22, 102, 35);
		btnBack.addActionListener(this);
		add(btnBack);
		
		btnLogOut = new JButton("Log Out");
		btnLogOut.setSize(150, 30);
		btnLogOut.setBounds(393, 22, 78, 35);
		btnLogOut.addActionListener(this);
		add(btnLogOut);
		
		btnSave = new JButton("Track");
		btnSave.setBounds(319, 140, 97, 51);
		btnSave.addActionListener(this);
		add(btnSave);
		
		btnCancelOrder = new JButton("Cancel Order");
		btnCancelOrder.setEnabled(false);
		btnCancelOrder.setBounds(182, 220, 120, 35);
		btnCancelOrder.addActionListener(this);
		add(btnCancelOrder);
		
		VehicleNumber = new JFormattedTextField();
		VehicleNumber.setBounds(219, 171, 90, 20);
		add(VehicleNumber);
		
		lblVehicleNumber = new JLabel("Vehicle Number :");
		lblVehicleNumber.setBounds(108, 174, 160, 14);
		add(lblVehicleNumber);
		
		JLabel lblIDNumber = new JLabel("ID Number :");
		lblIDNumber.setBounds(108, 140, 160, 14);
		add(lblIDNumber);
		
		idNumber = new JFormattedTextField();
		idNumber.setBounds(219, 140, 90, 20);
		add(idNumber);
		
		btnEnter = new JButton("Enter Vehicle");
		btnEnter.setBounds(182, 266, 120, 35);
		btnEnter.addActionListener(this);
		add(btnEnter);
	}
	
	
	
	/**
	 * This method listens to the buttons on the panel
	 * @param e is the buttons the user Click
	 */
	@Override
	public void actionPerformed(ActionEvent e)
	{

		if (e.getSource() == btnSave)
		{
			idNumber.setBackground(Color.WHITE);
			VehicleNumber.setBackground(Color.WHITE);
			
			if (FieldValidator.checkNumeric(idNumber.getText()) && FieldValidator.checkNumeric(VehicleNumber.getText()))
			{
				Order order=new Order();	
				order.setClientNumber(Integer.parseInt(idNumber.getText()));
				if (VehicleNumber.getText().equals(""))
					order.setVehicleNumber(-1);
				else order.setVehicleNumber(Integer.parseInt(VehicleNumber.getText()));
				SystemTypes orderToTrack = new SystemTypes(order,null);
				SystemTypes trackResult = orderControl.trackControl(orderToTrack);
				if (trackResult.getSysMode().equals(SystemMode.OK))
					{
					btnCancelOrder.setEnabled(true);
					JOptionPane.showMessageDialog(null, trackResult.getObj().toString(), "Success",JOptionPane.INFORMATION_MESSAGE);
					}
				else
					JOptionPane.showMessageDialog(null,trackResult.getObj().toString(), "Error",JOptionPane.ERROR_MESSAGE);
			}
			else{
				if (!FieldValidator.checkNumeric(idNumber.getText())) idNumber.setBackground(Color.RED);
				if (!FieldValidator.checkNumeric(VehicleNumber.getText())) VehicleNumber.setBackground(Color.RED);
				JOptionPane.showMessageDialog(null,"Invalid field(s) detected !", "Error",JOptionPane.ERROR_MESSAGE);
			}

		}
		if (e.getSource() == btnEnter)
		{
			Order order = new Order();
			order.setClientNumber(Integer.parseInt(idNumber.getText()));
			order.setVehicleNumber(Integer.parseInt(VehicleNumber.getText()));
			order.setSidParkingLocation(VCP.currentLot.getParkingLotNumber());
			SystemTypes orderToEnter = new SystemTypes(order,null);
			SystemTypes enterResult = localOpControl.enterCar(orderToEnter);
			if (enterResult.getSysMode().equals(SystemMode.OK))
				{
					JOptionPane.showMessageDialog(null, enterResult.getObj().toString(), "Success",JOptionPane.INFORMATION_MESSAGE);
				}
			else
				JOptionPane.showMessageDialog(null,enterResult.getObj().toString(), "Error",JOptionPane.ERROR_MESSAGE);
		}
		if (e.getSource() == btnCancelOrder)
		{
			Order order = new Order();
			order.setOrderStatus("CANCELED");
			order.setClientNumber(Integer.parseInt(idNumber.getText()));
			order.setVehicleNumber(Integer.parseInt(VehicleNumber.getText()));
			
			SystemTypes orderToCancel = new SystemTypes(order,null);
			SystemTypes setControlResult = orderControl.cancelOrder(orderToCancel);
			if (setControlResult.getSysMode().equals(SystemMode.OK))
				JOptionPane.showMessageDialog(null, setControlResult.getObj().toString(), "Success",JOptionPane.INFORMATION_MESSAGE);
			else
				JOptionPane.showMessageDialog(null, setControlResult.getObj().toString(), "Error",JOptionPane.ERROR_MESSAGE);
		}
		
		if (e.getSource() == btnBack)
		{
			VCP.setScreen(this, VCP.prevScreen);
			VCP.prevScreen.setBounds(VCP.guiX,VCP.guiY,500,400);
			VCP.prevScreen.setLayout(null);
			VCP.frame.setBounds(VCP.prevScreen.getBounds());
		}
		
		if (e.getSource() == btnLogOut)
		{
			SystemTypes userToLogout = new SystemTypes(VCP.currentUser,null);
			boolean logoutRes = LoginController.LogOut(userToLogout);
			if (logoutRes)
				VCP.setGUI(0, this);
			else{
				JOptionPane.showMessageDialog(null, "Failed to logout!", "Error",
	                    JOptionPane.ERROR_MESSAGE);
			}
		}
	}
}
